package com.JFapp.annotation.validator;

import com.JFapp.annotation.validator.name.Unique;

import com.JFapp.domain.entity.User;
import com.JFapp.service.UserService;
import com.JFapp.utils.SecurityUtils;
import com.alibaba.excel.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.security.Security;

public class UniqueValidator implements ConstraintValidator<Unique,String> {

    @Autowired
    private UserService userService;
    @Override
    public boolean isValid(String ep, ConstraintValidatorContext constraintValidatorContext) {

        if (StringUtils.isBlank(ep)) {
            return false;
        }
        User user;
        if (ep.endsWith("com")){
            user = userService.findUserByEmail(ep);
        }else{
            user = userService.findUserByPhone(ep);
        }
        if ( user== null || user.getId()==SecurityUtils.getUserId()) {
            return true;
        }
        return  false;
    }

}

